<?php
namespace Admin\Controller;
class FriendController extends AdminController {
	public function index(){
		$title       =   I('title');
        $map['status']  =   array('egt',0);
        if(is_numeric($title)){
            $map['id|title']=   array(intval($title),array('like','%'.$title.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$title.'%');
        }

        $list   = $this->lists('Friendurl', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友情列表';
        $this->display();
	}

	public function add(){
		if(IS_POST){
			$friend = D('Friend');
            $res = $friend->create();
            if($res){
               if($friend->add()){
               	    $this->success('新增成功');
               }else{
               	    $this->error('新增失败');
               }
            }else{
            	$this->error($friend->getError());
            }
		}else{
			$this->meta_title = '新增友链';
			$this->display();
		}
		
	}

	public function update(){
		$friend = D('Friend');

		if(IS_POST){
			$res = $friend->create();
			if($res){
				if($friend->save()){
					$this->success('编辑成功');
				}else{
					$this->error('编辑失败');
				}
			}else{
				$this->error($friend->getError());
			}
		}else{
			$res = $friend->find(I('get.id'));
			$this->assign('res',$res);
			$this->meta_title = '编辑友链';
			$this->display();
		}
	}

    public function setStatus($model='Friend'){
        return parent::setStatus($model);
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbidfriend':
                $this->forbid('Friend', $map );
                break;
            case 'resumefriend':
                $this->resume('Friend', $map );
                break;
            case 'deletefriend':
                $this->delete('Friend', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

}

?>